package com.mcjtf.prefix.bridge;

import com.mcjtf.prefix.PrefixManager;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

public class VaultHook {
    private final PrefixManager plugin;
    private Permission permission;
    private Chat chat;

    public VaultHook() {
        plugin = PrefixManager.getInstance();
        RegisteredServiceProvider<Permission> permissionProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class);
        if (permissionProvider != null)
            permission = permissionProvider.getProvider();
        RegisteredServiceProvider<Chat> chatProvider = plugin.getServer().getServicesManager().getRegistration(Chat.class);
        if (chatProvider != null)
            chat = chatProvider.getProvider();
    }

    public String getGroupName(String name) {
        for (String g : permission.getGroups()) {
            if (g.equalsIgnoreCase(name))
                return g;
        }
        return null;
    }

    public String getPrefix(Player p) {
        return chat.getPlayerPrefix(p);
    }

    public String getSuffix(Player p) {
        return chat.getPlayerSuffix(p);
    }

    public String getGroupPrefix(String group, String world) {
        return chat.getGroupPrefix(world, group);
    }

    public String getGroupSuffix(String group, String world) {
        return chat.getGroupSuffix(world, group);
    }

    public String getGroup(Player p) {
        return this.permission.getPrimaryGroup(p);
    }

    @SuppressWarnings("deprecation")
    public String getGroup(String name, String world) {
        return permission.getPrimaryGroup(plugin.getServer().getWorld(world), name);
    }
}
